#include "client.h"

int main(int argc, char* argv[])
{   
    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(sockfd,-1,"socket");
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd,(struct sockaddr *)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");
    printf("The client is ready!\n");
    fflush(stdout);

    train_t sendTrain;
    train_t recvTrain;
    recvn(sockfd,&recvTrain.length,sizeof(int));
    recvn(sockfd,sendTrain.token,recvTrain.length);

    int userflag = userinfo(sockfd);
    if(userflag == 2){
        send(sockfd,&userflag,0,0);
        close(sockfd);
        return 0;
    }

    printf("Welcome The Winning Team system \n");
    fflush(stdout);

    char CMD[128] = {0};
    char buffer[4096] = {0};
    char *part1 = (char*)malloc(1024*sizeof(char));
    char *part2 = (char*)malloc(1024*sizeof(char));
    while(1){
        fflush(stdin);
        fflush(stdout);
        part1 = NULL;
        part2 = NULL;
        bzero(sendTrain.buf,sizeof(sendTrain.buf));
        bzero(recvTrain.buf,sizeof(recvTrain.buf));
        bzero(CMD,sizeof(CMD));
        bzero(buffer,sizeof(buffer));

        read(STDIN_FILENO,CMD,sizeof(CMD));
        fflush(stdin);
        strtok(CMD,"\n");
        if(strncmp(CMD,"cd",2) == 0){
            printf("CMD : cd\n");
            fflush(stdout);

            part1 = strtok_r(CMD," ",&part2);
            sendTrain.status = OD_CD;
            sendTrain.length = 2*sizeof(int)+sizeof(sendTrain.token)+strlen(part2);
            strcpy(sendTrain.buf,part2);
            send(sockfd,&sendTrain.status,sizeof(int),MSG_NOSIGNAL);
            send(sockfd,&sendTrain.length,sizeof(int),MSG_NOSIGNAL);
            send(sockfd,sendTrain.buf,sendTrain.length,MSG_NOSIGNAL);
            recvn(sockfd,&recvTrain.length,sizeof(int));
            if(recvTrain.length == -1){
                printf("CMD : cd Directory input error");
                fflush(stdout);
                continue;
            }
            recvn(sockfd,recvTrain.buf,recvTrain.length);
            strcpy(buffer,recvTrain.buf);
            printf("%s\n",buffer);
            fflush(stdout);
        }else if(strncmp(CMD,"ls",2) == 0){
            printf("CMD : ls\n");
            fflush(stdout);

            sendTrain.status = OD_LS;
            send(sockfd,&sendTrain.status,sizeof(int),MSG_NOSIGNAL);
            
            recvn(sockfd,&recvTrain.length,sizeof(int));
            if(recvTrain.length == -1){
                printf("CMD : ls Directory input error");
                fflush(stdout);
                continue;
            }
            recvn(sockfd,recvTrain.buf,recvTrain.length);
            strcpy(buffer,recvTrain.buf);
            printf("%s\n",buffer);
            fflush(stdout);
        }else if(strncmp(CMD,"puts",4) == 0){
            printf("CMD : puts\n");
            fflush(stdout);

            part1 = strtok_r(CMD," ",&part2);
            sendTrain.status = OD_PTS;
            sendTrain.length = strlen(part2);
            strcpy(sendTrain.buf,part2);
            send(sockfd,&sendTrain.status,sizeof(int),MSG_NOSIGNAL);

            srfile_t srfile;
            srfile.addr = addr;
            srfile.train = sendTrain;
            pthread_t tid;
            pthread_create(&tid,NULL,sendFilefunc,&srfile);
        }else if(strncmp(CMD,"gets",4) == 0){
            printf("CMD : gets\n");
            fflush(stdout);

            part1 = strtok_r(CMD," ",&part2);
            sendTrain.status = OD_GTC;
            sendTrain.length = strlen(part2);
            strcpy(sendTrain.buf,part2);
            send(sockfd,&sendTrain.status,sizeof(int),MSG_NOSIGNAL);

            srfile_t srfile;
            srfile.addr = addr;
            srfile.train = sendTrain;
            pthread_t tid;
            pthread_create(&tid,NULL,recvFilefunc,&srfile);
        }else if(strncmp(CMD,"rmdir",5) == 0){
            printf("CMD : rmdir\n");
            fflush(stdout);

            part1 = strtok_r(CMD," ",&part2);
            sendTrain.status = OD_RD;
            sendTrain.length = strlen(part2);
            strcpy(sendTrain.buf,part2);
            send(sockfd,&sendTrain.status,sizeof(int),MSG_NOSIGNAL);
            send(sockfd,&sendTrain.length,sizeof(int),MSG_NOSIGNAL);
            send(sockfd,sendTrain.buf,sendTrain.length,MSG_NOSIGNAL);

            recvn(sockfd,&recvTrain.length,sizeof(int));
            if(recvTrain.length == 0){
                printf("Rmdir success\n");
                fflush(stdout);
                continue;
            }else{
                printf("Rmdir failed,please try again\n");
                fflush(stdout);
                continue;
            }
        }else if(strncmp(CMD,"mkdir",5) == 0){
            printf("CMD : mkdir\n");
            fflush(stdout);

            part1 = strtok_r(CMD," ",&part2);
            sendTrain.status = OD_MD;
            sendTrain.length = strlen(part2);
            strcpy(sendTrain.buf,part2);
            send(sockfd,&sendTrain.status,sizeof(int),MSG_NOSIGNAL);
            send(sockfd,&sendTrain.length,sizeof(int),MSG_NOSIGNAL);
            send(sockfd,sendTrain.buf,sendTrain.length,MSG_NOSIGNAL);

            recvn(sockfd,&recvTrain.length,sizeof(int));
            if(recvTrain.length == 0){
                printf("Mkdir success\n");
                fflush(stdout);
                continue;
            }else{
                printf("Mkdir failed,please try again\n");
                fflush(stdout);
                continue;
            }
        }else if(strncmp(CMD,"rm",2) == 0){
            printf("CMD : rm\n");
            fflush(stdout);
            part1 = strtok_r(CMD," ",&part2);
            sendTrain.status = OD_RM;
            sendTrain.length = strlen(part2);
            strcpy(sendTrain.buf,part2);
            send(sockfd,&sendTrain.status,sizeof(int),MSG_NOSIGNAL);
            send(sockfd,&sendTrain.length,sizeof(int),MSG_NOSIGNAL);
            send(sockfd,sendTrain.buf,sendTrain.length,MSG_NOSIGNAL);
            recvn(sockfd,&recvTrain.length,sizeof(int));
            if(recvTrain.length == -1){
                printf("CMD : rm Directory input error\n");
                fflush(stdout);
                continue;
            }else{
                printf("rm %s success\n",part2);
                fflush(stdout);
                continue;
            }
        }else if(strncmp(CMD,"pwd",3) == 0){
            printf("CMD : pwd\n");
            fflush(stdout);

            part1 = strtok_r(CMD," ",&part2);
            sendTrain.status = OD_PWD;
            send(sockfd,&sendTrain,sizeof(sendTrain),MSG_NOSIGNAL);
            
            recvn(sockfd,&recvTrain,sizeof(recvTrain));
            if(recvTrain.length == -1){
                printf("CMD : pwd Directory input error\n");
                fflush(stdout);
                continue;
            }
            strcpy(buffer,recvTrain.buf);
            printf("%s\n",buffer);
            fflush(stdout);
        }else if(strncmp(CMD,"exit",4) == 0){
            printf("CMD : exit\n");
            fflush(stdout);
            send(sockfd,sendTrain.buf,0,0);
            printf("Welcome to use The Winning Team system next time \n");
            fflush(stdout);
            
            free(part1);
            free(part2);
            close(sockfd);
            return 0;
        }else if(strncmp(CMD,"logout",6) == 0){
            printf("CMD : logout\n");
            fflush(stdout);
            sendTrain.status = OD_LOGO;
            
            printf("Please enter the password to confirm :\n");
            fflush(stdout);
            read(STDIN_FILENO,sendTrain.buf,sizeof(sendTrain.buf));
            fflush(stdin);
            sendTrain.length = strlen(sendTrain.buf);
            send(sockfd,&sendTrain,sizeof(sendTrain.status)+sizeof(sendTrain.length)+sendTrain.length,MSG_NOSIGNAL);

            recvn(sockfd,&recvTrain,sizeof(recvTrain));
            if(recvTrain.length == 0){
                send(sockfd,&sendTrain.length,0,0);
                printf("The user logout success \n");
                fflush(stdout);
                free(part1);
                free(part2);
                close(sockfd);
                return 0;
            }else{
                printf("Logout failed \n");
                fflush(stdout);
                continue;
            }
        }
    }
    return 0;
}

